﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Phoenix.Xna.Framework.FrameworkExtensions
{
    public static class Vector3Extensions
    {
        public static Vector3 Inverse(this Vector3 obj)
        {
            return new Vector3(-obj.X, -obj.Y, -obj.Z);
        }

        public static Vector3 GetDirection(this Vector3 obj, Vector3 destination)
        {
            Vector3 direction = destination - obj;
            direction.Normalize();
            return direction;
        }

        public static Matrix ToRotation(this Vector3 obj)
        {
            return Matrix.CreateFromYawPitchRoll(obj.X, obj.Y, obj.Z);
        }

        public static Vector4 ToVector4(this Vector3 obj)
        {
            return new Vector4(obj.X, obj.Y, obj.Z, 1.0f);
        }

        public static Vector2 ToVector2(this Vector3 obj)
        {
            return new Vector2(obj.X, obj.Z);
        }

        public static Vector3 AngleTo(this Vector3 from, Vector3 to)
        {
            Vector3 angle = new Vector3();
            Vector3 v3 = Vector3.Normalize(to - from);
            angle.X = (float)System.Math.Asin(v3.Y);
            angle.Y = (float)System.Math.Atan2(-v3.X, -v3.Z);
            return angle;
        }

        public static Color ToColor(this Vector3 obj)
        {
            byte r = byte.Parse(obj.X.ToString());
            byte g = byte.Parse(obj.Y.ToString());
            byte b = byte.Parse(obj.Z.ToString());
            return new Color(r, g, b);
        }

        public static Vector3 FromColor(this Vector3 obj)
        {
            Vector3 v = new Vector3();
            v.X = obj.X * 255;
            v.Y = obj.Y * 255;
            v.Z = obj.Z * 255;
            return v;
        }
    }
}